package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaAccountViewDao;
import com.qymerge.manage.base.dao.IMetaPeriodDao;
import com.qymerge.manage.base.ro.MetaPeriodRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import com.qymerge.manage.core.utils.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *<p>Title: CardDaoImpl</p>
 *<p>Description: 优惠券管理</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 17:54
 */
@Repository
public class MetaPeriodDaoImpl extends BaseDao implements IMetaPeriodDao {

    private String dwd_MCRS_MDM_meta_Period = "SELECT Id,Code,PeriodYear,PeriodQuarter,PeriodMonth,BeginDate,EndDate,Actived,Etl_Last_Date FROM dwd_MCRS_MDM_meta_Period WHERE 1=1 ";

    @Override
    public List list(MetaPeriodRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_Period;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getId())) {
            sql += " and Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql += " and Code like :Code";
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if(ro.getPeriodYear() > -1){
            sql += " and PeriodYear >= :PeriodYear";
            paramMap.put("PeriodYear", ro.getPeriodYear());
        }
        if(ro.getPeriodQuarter() > -1){
            sql += " and PeriodQuarter >= :PeriodQuarter";
            paramMap.put("PeriodQuarter", ro.getPeriodQuarter());
        }
        if(ro.getPeriodMonth() > -1){
            sql += " and PeriodMonth >= :PeriodMonth";
            paramMap.put("PeriodMonth", ro.getPeriodMonth());
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql += " and BeginDate >= :create_time_begin AND EndDate <= :create_time_end";
            paramMap.put("create_time_begin", ro.getCreate_time_begin());
            paramMap.put("create_time_end", ro.getCreate_time_end());
        }

        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaPeriodRO>(MetaPeriodRO.class));
    }

    @Override
    public int total(MetaPeriodRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_Period where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getId())) {
            sql += " and Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql += " and Code like :Code";
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if(ro.getPeriodYear() > -1){
            sql += " and PeriodYear >= :PeriodYear";
            paramMap.put("PeriodYear", ro.getPeriodYear());
        }
        if(ro.getPeriodQuarter() > -1){
            sql += " and PeriodQuarter >= :PeriodQuarter";
            paramMap.put("PeriodQuarter", ro.getPeriodQuarter());
        }
        if(ro.getPeriodMonth() > -1){
            sql += " and PeriodMonth >= :PeriodMonth";
            paramMap.put("PeriodMonth", ro.getPeriodMonth());
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql += " and BeginDate >= :create_time_begin AND EndDate <= :create_time_end";
            paramMap.put("create_time_begin", ro.getCreate_time_begin());
            paramMap.put("create_time_end", ro.getCreate_time_end());
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public MetaPeriodRO findOne(String id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_Period);
        sql.append("and Id=?");
        RowMapper<MetaPeriodRO> rowMapper = new BeanPropertyRowMapper<MetaPeriodRO>(MetaPeriodRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }


}
